from loguru import logger
import sys
import os

LOG_FILE = 'ai_translation.log'
ROTATION_TIME = "02:00"


class Logger:
    def __init__(self, name="ai_translation", log_dir="logs", debug=False):
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        log_file_path = os.path.join(log_dir, LOG_FILE)
        # 移除默认的loguru的logger，避免重复输出日志
        logger.remove()

        # 设置日志输出级别
        level = "DEBUG" if debug else "INFO"
        logger.add(sys.stdout, level=level)

        # 设置日志文件的输出级别和轮转时间
        logger.add(log_file_path, rotation=ROTATION_TIME, level="DEBUG")
        # 设置logger的名字
        self.logger = logger


LOG = Logger(debug=True).logger

# 测试日志功能
if __name__ == '__main__':
    log = Logger().logger

    log.debug("This is a debug message")
    log.info("This is an info message")
    log.warning("This is a warning message")
    log.error("This is an error message")
